<?php
	include_once './inc/isLogin.php';
	//准备装载数据
	require_once("./../includes/configure.php");
	require_once("./../includes/database.php");
	$db = Database::Connect();
	
	if($_GET['conf']!='zufang_shangquan'){
		//更新,添加
		if($_GET['conf']=='zufang_quyu'){
			$this_key="zufang_quyu";
			$this_title="区域设置";
		}elseif($_GET['conf']=='zufang_xianlu'){
			$this_key="zufang_xianlu";
			$this_title="地铁线路设置";
		}elseif($_GET['conf']=='zufang_zujin'){
			$this_key="zufang_zujin";
			$this_title="租房租金设置";
		}elseif($_GET['conf']=='ershoufang_shoujia'){
			$this_key="ershoufang_shoujia";
			$this_title="二手房售价设置";
		}elseif($_GET['conf']=='ershoufang_mianji'){
			$this_key="ershoufang_mianji";
			$this_title="二手房面积设置";
		}elseif($_GET['conf']=='zufang_leixing'){
			$this_key="zufang_leixing";
			$this_title="房屋类型设置";
			$add_this_title="添加房屋类型";
			$has_this_title="已有房屋类型";
		}elseif($_GET['conf']=='zufang_fangxing'){
			$this_key="zufang_fangxing";
			$this_title="房屋户型设置";
			$add_this_title="添加房屋户型";
			$has_this_title="已有房屋户型";
		}elseif($_GET['conf']=='qiugou_chanquan'){
			$this_key="qiugou_chanquan";
			$this_title="求购产权设置";
			$add_this_title="添加求购产权";
			$has_this_title="已设置的求购产权";
		}elseif($_GET['conf']=='sms'){
			$this_key="sms-switch";
			$this_title="全站短信功能设置";
			$add_this_title="打开/关闭短信功能";
			$has_this_title="目前状态";
		}
		if($_POST['submit']){
			if($_POST['act']=='add'){
				$value = $_POST['value'];
				$C = $_POST['C'];
				if($_GET['conf']=='zufang_zujin' ||$_GET['conf']=='ershoufang_shoujia' || $_GET['conf']=='ershoufang_mianji' ||$_GET['conf']=='zufang_leixing' || $_GET['conf']=='zufang_fangxing'|| $_GET['conf']=='qiugou_chanquan'){
					$sql = "insert into configuration (this_id,description,`key`,`value`,C)
							values(null,'备注难得分了','$this_key','$value','$C')";  
				}else{
					$sql = "insert into configuration (this_id,description,`key`,`value`)
						values(null,'备注难得分了','$this_key','$value')";   
				}
				$db->Execute($sql);
				$update_result = '添加完成';
			}else if($_POST['act']=='update'){
				$this_id = $_POST['this_id'];
				$value = $_POST['value'];
				$C = $_POST['C'];
				if($_GET['conf']=='zufang_zujin' ||$_GET['conf']=='ershoufang_shoujia' ||$_GET['conf']=='ershoufang_mianji' || $_GET['conf']=='zufang_leixing' || $_GET['conf']=='zufang_fangxing'|| $_GET['conf']=='qiugou_chanquan'){
					$sql = "update configuration set `value`='$value',C='$C' WHERE this_id=$this_id";   
				}else{
					$sql = "update configuration set `value`='$value' WHERE this_id=$this_id";   
				}  
				$db->Execute($sql);
				$update_result = '更新完成';
			}else if($_POST['act']=='del'){
				$this_id = $_POST['this_id'];
				$sql = "delete from configuration WHERE this_id=$this_id";   
				$db->Execute($sql);
				$update_result = '成功删除';
			}
		}
		//end 更新,添加
		$sql = "SELECT * FROM configuration WHERE `key`='$this_key'"; 
		$conf_result = $db->GetResultSet($sql);
	}elseif($_GET['conf']=='zufang_shangquan'){
		$this_title="商圈设置";
		if($_POST['submit']){
			if($_POST['act']=='add'){
				$value = $_POST['value'];
				$quyu_id = $_POST['quyu_id'];
				$sql = "insert into configuration (this_id,description,`key`,`value`,A)
						values(null,'租房_商圈','zufang_shangquan','$value',$quyu_id)";   
				$db->Execute($sql);
				$update_result = '添加完成';
			}else if($_POST['act']=='update'){
				$this_id = $_POST['this_id'];
				$value = $_POST['value'];
				$quyu_id = $_POST['quyu_id'];
				$sql = "update configuration set `value`='$value',A=$quyu_id WHERE this_id=$this_id";   
				$db->Execute($sql);
				$update_result = '更新完成';
			}else if($_POST['act']=='del'){
				$this_id = $_POST['this_id'];
				$sql = "delete from configuration WHERE this_id=$this_id";   
				$db->Execute($sql);
				$update_result = '成功删除';
			}
		}
		$sql = "select B.`value` as quyu,B.this_id as quyu_id,A.* from configuration A
				left join configuration B
				on A.A = B.this_id
				where A.`key` = 'zufang_shangquan'
				ORDER BY B.`value`"; 
		$conf_result = $db->GetResultSet($sql);
	}
?>
<div class="top-bar" style=" border-bottom: 2px solid #FF7700;padding:10px;">
	<?//<a href="#" class="button">ADD NEW </a>?>
	<h1><?php echo $this_title;?></h1>
</div><br />

<br/><br/>
<?php
	if($update_result){
		if($_GET['conf']!='sms'){
			echo '<br/><span class="info green">'.$update_result.'</span><br/>';
		}
	}
	if($_GET['conf']=='zufang_quyu'){
?>
			<div style="padding: 10px;">
				一、添加区域<br/><br/>
				<form action="" method="post">
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="value" value="">
					<input type="hidden" name="act" value="add">
					<input type="submit" name="submit" value="添加">
				</form>
			</div>
			<div style="padding: 10px;">
				二、已有区域<br/><br/>
			<?php
				$index_this = 1;
				while($row = mysql_fetch_array($conf_result)){
				?>
					<div style="clear:both;"></div>
					<form action="" method="post" style="float:left;">
						<?php echo $index_this++;?>.&nbsp;
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="text" name="value" value="<?php echo $row['value'];?>">
						<input type="hidden" name="act" value="update">
						<input type="submit" name="submit" value="修改">
					</form>
					<form action="" method="post">
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="hidden" name="act" value="del">
						<input type="submit" name="submit" value="删除">
					</form>
				<?php
					//echo $row['value'];
				}
			?>
			</div>

<?php 
	}elseif($_GET['conf']=='zufang_shangquan'){
?>

	<div style="padding: 10px;">
				一、添加商圈<br/><br/>
				<form action="" method="post">
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					所属区域:&nbsp;<select name="quyu_id">
						<?php
							$zufang_quyu_array = $db->getConfigurationArray('zufang_quyu');
							for($ii=0;$ii<count($zufang_quyu_array);$ii++){
								echo "<option value='".$zufang_quyu_array[$ii]['this_id']."'>&nbsp;&nbsp;".$zufang_quyu_array[$ii]['value']."&nbsp;&nbsp;</option>";
							}
						?>
					</select>
					<input type="text" name="value" value="">
					<input type="hidden" name="act" value="add">
					<input type="submit" name="submit" value="添加">
				</form>
			</div>
			<div style="padding: 10px;">
				二、现有商圈<br/><br/>
			<div style="width: 200px;padding-left:25px;">
			<?php
				$index_this = 1;
				$up_quyu_name = '';
				while($row = mysql_fetch_array($conf_result)){
				?>
					<div style="clear:both;"></div>
					<form action="" method="post" style="line-height:20px;">
						<?php echo $index_this++;?>.&nbsp;&nbsp;&nbsp;
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<span style="width:40px;"><?php 
							if($up_quyu_name==$row['quyu']){
								echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-';
							}else{
								$up_quyu_name=$row['quyu'];
								echo $row['quyu'].' - ';
							}
							?></span>
						<span style="width:40px;"><?php echo $row['value'];?></span>
						<input type="hidden" name="act" value="del">
						<input type="submit" name="submit" value="删除" style="float:right;">
					</form>
				<?php
					//echo $row['value'];
				}
			?>
			</div>
			</div>

<?php
	}elseif($_GET['conf']=='zufang_xianlu'){
?>
			<div style="padding: 10px;">
				一、添加线路<br/><br/>
				<form action="" method="post">
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="value" value="">
					<input type="hidden" name="act" value="add">
					<input type="submit" name="submit" value="添加">
				</form>
			</div>
			<div style="padding: 10px;">
				二、已有线路<br/><br/>
			<?php
				$index_this = 1;
				while($row = mysql_fetch_array($conf_result)){
				?>
					<div style="clear:both;"></div>
					<form action="" method="post" style="float:left;">
						<?php echo $index_this++;?>.&nbsp;&nbsp;&nbsp;
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="text" name="value" value="<?php echo $row['value'];?>">
						<input type="hidden" name="act" value="update">
						<input type="submit" name="submit" value="修改">
					</form>
					<form action="" method="post">
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="hidden" name="act" value="del">
						<input type="submit" name="submit" value="删除">
					</form>
				<?php
					//echo $row['value'];
				}
			?>
			</div>
<?php
	}elseif($_GET['conf']=='zufang_zujin'){
?>
			<div style="padding: 10px;">
				一、添加租金分类<br/><br/>
				<form action="" method="post">
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					数学描述:<input type="text" name="value" value="">
					页面描述:<input type="text" name="C" value="">
					<input type="hidden" name="act" value="add">
					<input type="submit" name="submit" value="添加">
				</form>
			</div>
			<div style="padding: 10px;">
				二、已有租金分类<br/><br/>
			<?php
				$index_this = 1;
				while($row = mysql_fetch_array($conf_result)){
				?>
					<div style="clear:both;"></div>
					<form action="" method="post" style="float:left;">
						<?php echo $index_this++;?>.&nbsp;&nbsp;&nbsp;
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="text" name="value" value="<?php echo $row['value'];?>">
						<input type="text" name="C" value="<?php echo $row['C'];?>">
						<input type="hidden" name="act" value="update">
						<input type="submit" name="submit" value="修改">
					</form>
					<form action="" method="post">
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="hidden" name="act" value="del">
						<input type="submit" name="submit" value="删除">
					</form>
				<?php
					//echo $row['value'];
				}
			?>
			</div>
<?php
	}elseif($_GET['conf']=='ershoufang_shoujia'){
?>
			<div style="padding: 10px;">
				一、添加售价分类<br/><br/>
				<form action="" method="post">
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					数学描述:<input type="text" name="value" value="">
					页面描述:<input type="text" name="C" value="">
					<input type="hidden" name="act" value="add">
					<input type="submit" name="submit" value="添加">
				</form>
			</div>
			<div style="padding: 10px;">
				二、已有售价分类<br/><br/>
			<?php
				$index_this = 1;
				while($row = mysql_fetch_array($conf_result)){
				?>
					<div style="clear:both;"></div>
					<form action="" method="post" style="float:left;">
						<?php echo $index_this++;?>.&nbsp;&nbsp;&nbsp;
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="text" name="value" value="<?php echo $row['value'];?>">
						<input type="text" name="C" value="<?php echo $row['C'];?>">
						<input type="hidden" name="act" value="update">
						<input type="submit" name="submit" value="修改">
					</form>
					<form action="" method="post">
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="hidden" name="act" value="del">
						<input type="submit" name="submit" value="删除">
					</form>
				<?php
					//echo $row['value'];
				}
			?>
			</div>
<?php
	}elseif($_GET['conf']=='ershoufang_mianji'){
?>
			<div style="padding: 10px;">
				一、添加面积分类<br/><br/>
				<form action="" method="post">
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					数学描述:<input type="text" name="value" value="">
					页面描述:<input type="text" name="C" value="">
					<input type="hidden" name="act" value="add">
					<input type="submit" name="submit" value="添加">
				</form>
			</div>
			<div style="padding: 10px;">
				二、已有面积分类<br/><br/>
			<?php
				$index_this = 1;
				while($row = mysql_fetch_array($conf_result)){
				?>
					<div style="clear:both;"></div>
					<form action="" method="post" style="float:left;">
						<?php echo $index_this++;?>.&nbsp;&nbsp;&nbsp;
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="text" name="value" value="<?php echo $row['value'];?>">
						<input type="text" name="C" value="<?php echo $row['C'];?>">
						<input type="hidden" name="act" value="update">
						<input type="submit" name="submit" value="修改">
					</form>
					<form action="" method="post">
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="hidden" name="act" value="del">
						<input type="submit" name="submit" value="删除">
					</form>
				<?php
					//echo $row['value'];
				}
			?>
			</div>
<?php
	}elseif($_GET['conf']=='zufang_leixing'||$_GET['conf']=='zufang_fangxing'|| $_GET['conf']=='qiugou_chanquan'){
?>
		<div style="padding: 10px;">
				一、<?php echo $add_this_title;?><br/><br/>
				<form action="" method="post">
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					数值唯一编号:<input type="text" name="value" value="">
					页面描述:<input type="text" name="C" value="">
					<input type="hidden" name="act" value="add">
					<input type="submit" name="submit" value="添加">
				</form>
			</div>
			<div style="padding: 10px;">
				二、<?php echo $has_this_title;?><br/><br/>
			<?php
				$index_this = 1;
				while($row = mysql_fetch_array($conf_result)){
				?>
					<div style="clear:both;"></div>
					<form action="" method="post" style="float:left;">
						<?php echo $index_this++;?>.&nbsp;&nbsp;&nbsp;
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="text" name="value" value="<?php echo $row['value'];?>">
						<input type="text" name="C" value="<?php echo $row['C'];?>">
						<input type="hidden" name="act" value="update">
						<input type="submit" name="submit" value="修改">
					</form>
					<form action="" method="post">
						<input type="hidden" name="this_id" value="<?php echo $row['this_id'];?>">
						<input type="hidden" name="act" value="del">
						<input type="submit" name="submit" value="删除">
					</form>
				<?php
					//echo $row['value'];
				}
			?>
			</div>
<?php
	}elseif($_GET['conf']=='sms'){
?>
		<div style="padding: 10px;">
				一、<?php echo $has_this_title;?><br/><br/>
				<?php
					$sms_state = -1;
					$this_id = -1;
					while($row = mysql_fetch_array($conf_result)){
						$this_id = $row['this_id'];
						if($row['value']==1){
							echo '<span style="font-size:18px;color:green;font-weight:bold;padding-left:20px;">运行中</span>';
							$sms_state = 1;
						}elseif($row['value']==0){
							echo '<span style="font-size:18px;color:red;font-weight:bold;padding-left:20px;">已关闭</span>';
							$sms_state = 0;
						}
					}
				?>
			</div>
			<div style="padding: 10px;">
				二、<?php echo $add_this_title;?><br/><br/>
			
					<form action="" method="post">
						<input type="hidden" name="this_id" value="<?php echo $this_id;?>">
						<input type="hidden" name="value" value="<?php echo $sms_state?'0':'1'?>">
						<input type="hidden" name="act" value="update">
						<input type="submit" name="submit" value="切换状态" style="font-size:18px;color:green;font-weight:bold;margin-left:20px; border:none;cursor: pointer;background-color: #FB8200; border-radius:4px;">
					</form>
			</div>
<?php
	}
	$db->close();
?>